//
//  KDDPosition.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 ZHBen. All rights reserved.
//
#import "KDDPosition.h"
@interface KDDPosition()

@property (nonatomic) BOOL isValid;

@end

@implementation KDDPosition
KNSCopyingImpl

- (instancetype)initWithViewType:(KDDViewType)viewType section:(NSInteger)section index:(NSInteger)index
{
    if (self = [super init])
    {
        _viewType = viewType;
        _section = section;
        _index = index;
    }
    return self;
}

- (NSIndexPath *)indexPath
{
    if (_indexPath == nil)
    {
        _indexPath = [NSIndexPath indexPathForRow:_index inSection:_section];
    }
    return _indexPath;
}

- (void)setIndex:(NSInteger)index
{
    if (index != _index)
    {
        _index = index;
        _indexPath = nil;
    }
}

- (void)setSection:(NSInteger)section
{
    if (section != _section)
    {
        _section = section;
        _indexPath = nil;
    }
}

+ (KDDPosition *)cellPosition:(NSIndexPath *)indexPath
{
    return [self cellPositionWithSection:indexPath.section index:indexPath.row];
}

+ (KDDPosition *)cellPositionWithSection:(NSInteger)section index:(NSInteger)index
{
    return [[self alloc] initWithViewType:KDDViewTypeCell section:section index:index];
}

+ (KDDPosition *)headerPosition:(NSInteger)section
{
    return [[self alloc] initWithViewType:KDDViewTypeHeader section:section index:0];
}

+ (KDDPosition *)footerPosition:(NSInteger)section
{
    return [[self alloc] initWithViewType:KDDViewTypeFooter section:section index:0];
}

+ (KDDPosition *)positionWithViewType:(KDDViewType)viewType indexPath:(NSIndexPath *)indexPath
{
    KDDPosition * position = [[self alloc] initWithViewType:viewType section:indexPath.section index:indexPath.row];
    position.indexPath = indexPath;
    return position;
}

+ (NSArray<KDDPosition *> *)cellPositionList:(NSIndexSet *)indexSet section:(NSInteger)section
{
    NSMutableArray<KDDPosition *> * positionList = [NSMutableArray arrayWithCapacity:indexSet.count];
    [indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
        KDDPosition * position = [[KDDPosition alloc] initWithViewType:KDDViewTypeCell section:section index:idx];
        [positionList addObject:position];
    }];
    return positionList;
}

+ (NSArray<KDDPosition *> *)sortedPositionList:(NSArray<KDDPosition *> *)positionList
{
    return [positionList sortedArrayUsingComparator:^NSComparisonResult(KDDPosition * obj1, KDDPosition * obj2) {
        return [obj1 compare:obj2];
    }];
}

- (BOOL)isEqual:(KDDPosition *)object
{
    return self.section == object.section && self.viewType == object.viewType && self.index == object.index;
}

- (NSComparisonResult)compare:(KDDPosition *)object
{
    NSComparisonResult result = [@(self.section) compare:@(object.section)];
    if (result == NSOrderedSame)
    {
        KDDViewType viewType1 = self.viewType;
        if (viewType1 == KDDViewTypeDecoration)
        {
            viewType1 = KDDViewTypeCell;
        }
        KDDViewType viewType2 = object.viewType;
        if (viewType2 == KDDViewTypeDecoration)
        {
            viewType2 = KDDViewTypeCell;
        }
        result = [@(viewType1) compare:@(viewType2)];
        if (result == NSOrderedSame)
        {
            result = [@(self.index) compare:@(object.index)];
        }
    }
    return result;
}

@end
